package com.wenwei.sharebox.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wenwei.sharebox.R;
import com.wenwei.sharebox.activity.GoodsDetailsActivity;
import com.wenwei.sharebox.activity.ReturnGoodsActivity;
import com.wenwei.sharebox.activity.ReturnMoneyActivity;
import com.wenwei.sharebox.activity.ShareHistoryActivity;
import com.wenwei.sharebox.adapter.ShareZuAdapter;
import com.wenwei.sharebox.adapter.UserShareAdapter;
import com.wenwei.sharebox.base.BaseFragment;
import com.wenwei.sharebox.bean.UserShareBean;
import com.wenwei.sharebox.net.ApiManager;
import com.wenwei.sharebox.net.BaseSubscriber;
import com.wenwei.sharebox.utils.UserStringUtils;
import com.wenwei.sharebox.view.LoginPopup;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/7/6.
 */
public class ShareFragment extends BaseFragment {
    @Bind(R.id.share_buy_tv)
    TextView shareBuyTv;
    @Bind(R.id.share_zu_tv)
    TextView shareZuTv;
    @Bind(R.id.content_ll)
    LinearLayout mContentLl;

    private FragmentManager mFragmentManager;
    private FragmentTransaction mTransaction;

    private ShareBuyFragment buyFragment;
    private ShareRentFragment rentFragment;

    @Override
    public View initView(LayoutInflater inflater) {
        return LayoutInflater.from(mActivity).inflate(R.layout.fragment_share, null);
    }

    @Override
    protected void lazyload() {

    }



    @Override
    protected void initEvent() {
        super.initEvent();

        if(UserStringUtils.getUserToken(mActivity).equals("")){
            showLoginPopup();
        }else {
            changeSelect(1);
        }
    }

    private void setTabSelection(int position){
        mTransaction = mFragmentManager.beginTransaction();
        hideFragment(mTransaction);
        switch(position){
            case 1:
                if (buyFragment == null) {
                    buyFragment = new ShareBuyFragment();
                    mTransaction.add(R.id.fragment_layout, buyFragment);
                } else {
                    mTransaction.show(buyFragment);
                }
                break;
            case 2:
                if (rentFragment == null) {
                    rentFragment = new ShareRentFragment();
                    mTransaction.add(R.id.fragment_layout, rentFragment);
                } else {
                    mTransaction.show(rentFragment);
                }
                break;

        }
        mTransaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (buyFragment != null) {
            transaction.hide(buyFragment);
        }
        if (rentFragment != null) {
            transaction.hide(rentFragment);
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        mFragmentManager = getFragmentManager();
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.share_buy_tv, R.id.share_zu_tv,R.id.share_history})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.share_buy_tv:
                changeSelect(1);
                break;
            case R.id.share_zu_tv:
                changeSelect(2);
                break;
            case R.id.share_history:
                goActivity(mActivity, ShareHistoryActivity.class);
                break;
        }
    }

    private void changeSelect(int position){
        if (position == 1){
            //购买
            shareBuyTv.setBackgroundResource(R.drawable.bc_share_select);
            shareBuyTv.setTextColor(Color.parseColor("#ffffff"));
            shareZuTv.setBackgroundResource(R.drawable.bc_share_normal);
            shareZuTv.setTextColor(Color.parseColor("#40CCCB"));
            setTabSelection(1);
        }else if (position == 2){
            //租用
            shareBuyTv.setBackgroundResource(R.drawable.bc_share_normal);
            shareBuyTv.setTextColor(Color.parseColor("#40CCCB"));
            shareZuTv.setBackgroundResource(R.drawable.bc_share_select);
            shareZuTv.setTextColor(Color.parseColor("#ffffff"));
            setTabSelection(2);
        }
    }


    private void showLoginPopup() {
        new LoginPopup(mActivity, mContentLl, new LoginPopup.CallBack() {
            @Override
            public void onForgetClick() {

            }

            @Override
            public void onLoginSuccess() {
                changeSelect(1);
            }
        });
    }
}
